// 引入vue、vuex
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

import axios from 'axios';

// 三个重要对象
const actions = {
    // 台湾表格数据
    getTaiwanData(context){
        context.commit('GETTAIWANLOADING',true);
        axios.get('https://api.baroncow.com/index.php/api/query/demoapi',{
            params:{
                area:'TW',
                limit:10000,
                beginTime:formatDate(context.state.taiwan.beginTime),
                endTime:formatDate(context.state.taiwan.endTime),
                event:context.state.taiwan.event
            }
        }).then(res=>{
            context.commit('GETTAIWANDATA',res.data);
            context.commit('GETTAIWANLOADING',false);
        })
    },
    // 香港表格数据
    getHongKongData(context){
        context.commit('GETHONGKONGLOADING',true);
        axios.get('https://api.baroncow.com/index.php/api/query/demoapi',{
            params:{
                area:'HK',
                limit:10000,
                beginTime:formatDate(context.state.hongkong.beginTime),
                endTime:formatDate(context.state.hongkong.endTime),
                event:context.state.hongkong.event
            }
        }).then(res=>{
            context.commit('GETHONGKONGDATA',res.data)
            context.commit('GETHONGKONGLOADING',false);
        })
    },
    // 日本表格数据
    getJapanData(context){
        context.commit('GETJAPANLOADING',true);
        axios.get('https://api.baroncow.com/index.php/api/query/demoapi',{
            params:{
                area:'JP',
                limit:10000,
                beginTime:formatDate(context.state.japan.beginTime),
                endTime:formatDate(context.state.japan.endTime),
                event:context.state.japan.event
            }
        }).then(res=>{
            context.commit('GETJAPANDATA',res.data)
            context.commit('GETJAPANLOADING',false);
        })
    },
    // 韩国表格数据
    getKoreaData(context){
        context.commit('GETKOREALOADING',true);
        axios.get('https://api.baroncow.com/index.php/api/query/demoapi',{
            params:{
                area:'KR',
                limit:10000,
                beginTime:formatDate(context.state.korea.beginTime),
                endTime:formatDate(context.state.korea.endTime),
                event:context.state.korea.event
            }
        }).then(res=>{
            context.commit('GETKOREADATA',res.data)
            context.commit('GETKOREALOADING',false);
        })
    },

    // 总览数据
    getDataScreen(context){
        axios.get('https://api.baroncow.com/index.php/api/query/demodatacount',{
            params:{
                'auth':'wotai'
            }
        }).then(res=>{
            context.commit('GETDATASCREEN',res.data);
        })
    },

    // 域名健康表格
    getTableHealthData(context){
        axios.get('https://api.baroncow.com/index.php/api/query/health?auth=wotai').then(res=>{
        // this.tabledata = res.data
            context.commit('GETTABLEHEALTHDATA',res.data);
      })
    }
};

const mutations = {
    // 台湾表格数据
    GETTAIWANDATA(state,value){
        if(!value.code){
            return
        }
        state.taiwan.tableData = value.data
    },
    // 香港表格数据
    GETHONGKONGDATA(state,value){
        if(!value.code){
            return
        }
        state.hongkong.tableData = value.data
    },
    // 日本表格数据
    GETJAPANDATA(state,value){
        if(!value.code){
            return
        }
        state.japan.tableData = value.data
    },
    // 韩国表格数据
    GETKOREADATA(state,value){
        if(!value.code){
            return
        }
        state.korea.tableData = value.data
    },

    // 台湾开始时间
    GETTAIWANBEGINTIME(state,value){
        state.taiwan.beginTime = value
    },
    // 台湾结束时间
    GETTAIWANENDTIME(state,value){
        state.taiwan.endTime = value
    },
    // 台湾事件类型
    GETTAIWANEVENT(state,value){
        state.taiwan.event = value
    },
    // 台湾表格是否加载中
    GETTAIWANLOADING(state,value){
        state.taiwan.isloading = value
    },

    // 香港开始时间
    GETHONGKONGBEGINTIME(state,value){
        state.hongkong.beginTime = value
    },
    // 香港结束时间
    GETHONGKONGENDTIME(state,value){
        state.hongkong.endTime = value
    },
    // 香港事件类型
    GETHONGKONGEVENT(state,value){
        state.hongkong.event = value
    },
    // 香港表格是否加载
    GETHONGKONGLOADING(state,value){
        state.hongkong.isloading = value
    },

    // 日本开始时间
    GETJAPANBEGINTIME(state,value){
        state.japan.beginTime = value
    },
    // 日本结束时间
    GETJAPANENDTIME(state,value){
        state.japan.endTime = value
    },
    // 日本事件类型
    GETJAPANEVENT(state,value){
        state.japan.event = value
    },
    // 日本表格是否加载
    GETJAPANLOADING(state,value){
        state.japan.isloading = value
    },

    // 韩国开始时间
    GETKOREABEGINTIME(state,value){
        state.korea.beginTime = value
    },
    // 韩国结束时间
    GETKOREAENDTIME(state,value){
        state.korea.endTime = value
    },
    // 韩国事件类型
    GETKOREAEVENT(state,value){
        state.korea.event = value
    },
    // 韩国表格是否加载
    GETKOREALOADING(state,value){
        state.korea.isloading = value
    },


    // 数据总览
    GETDATASCREEN(state,value){
        state.datascreen = value
    },
    // 域名健康表格
    GETTABLEHEALTHDATA(state,value){
        state.tableHealthData = value;
    }
};

const state = {
    // 域名健康表格
    tableHealthData:'',

    // 数据总览
    datascreen:'',

    // 台湾数据
    taiwan:{
        tableData:'',
        beginTime:'',
        endTime:'',
        event:'',
        isloading:false
    },

    // 香港数据
    hongkong:{
        tableData:'',
        beginTime:'',
        endTime:'',
        event:'',
        isloading:false
    },

    // 日本数据
    japan:{
        tableData:'',
        beginTime:'',
        endTime:'',
        event:'',
        isloading:false
    },

    // 韩国数据
    korea:{
        tableData:'',
        beginTime:'',
        endTime:'',
        event:'',
        isloading:false
    }
};

const getters = {
    taiwanLength(state){
        return state.taiwan.tableData.length
    },
    hongkongLength(state){
        return state.hongkong.tableData.length
    },
    japanLength(state){
        return state.japan.tableData.length
    },
    koreaLength(state){
        return state.korea.tableData.length
    }
}

// 辅助函数，用于格式化时间对象
function  formatDate(date) {
    if(!date){
        return null;
    }
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    const hours = String(date.getHours()).padStart(2, "0");
    const minutes = String(date.getMinutes()).padStart(2, "0");
    const seconds = String(date.getSeconds()).padStart(2, "0");

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})

